/**
 * Created with IntelliJ IDEA.
 * User: Администратор
 * Date: 01.07.12
 * Time: 3:28
 * To change this template use File | Settings | File Templates.
 */
require(
    [ "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "esri/map", "extras/SeatGeekUtils" ]
    );

var map;

      function init() {
        var ext = new esri.geometry.Extent({"xmin":-17106023,"ymin":2425274,"xmax":-5081561,"ymax":7503138,"spatialReference":{"wkid":102100}});
        map = new esri.Map("map",{ extent: ext, wrapAround180: true });
        var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer");
        map.addLayer(basemap);

        dojo.connect(map, "onLoad", function() {
          dojo.connect(dijit.byId("map"), "resize", map, map.resize);
        });
/*

        require(["dojo/on"], function(on) {
            on(map, "load", function() {
                require(["dojo/on"], function(on) {
                    on(dijit.byId("map"), "resize", map, map.resize);
                });
            });
        });
*/

        // instantiate the Seat Geek Search class
        var sg = new extras.SeatGeekSearch({
          "distance": "20mi",
          "perPage": 10
        });
        // search Seat Geek for events when the map is clicked
        dojo.connect(map, "onClick", function(e) {
          // Seat Geek expects latitude, longitude
          var geographic = esri.geometry.webMercatorToGeographic(e.mapPoint);
          // searchByLoc returns a deferred
          // once the deferred is resolved,
          // pass the results to a callback function
          var sgResults = sg.searchByLoc(geographic);
          sgResults.then(searchSucceeded, searchFailed);
        });
      }

      function searchSucceeded(results) {
        // log results from Seat Geek to the console
        if ( results ) {
          dojo.forEach(results.events, function(e) {
            console.log("Event: ", e.short_title, "; Average Price: ", e.stats.average_price);
          });
        } else {
          console.log("Search completed successfully but there were not any events found near that location.");
        }
      }

      function searchFailed(err) {
        console.log("Seat Geek error: ", error);
      }
      dojo.ready(init);

//var init = function() {
//    require(
//        ["dojo/on"], function(on) {
//            on(
//                dojo.byId("btnSayHello"),
//                "click",
//                helloButton_onClick
//            );
//        }
//    );
//}
//
////button onClick listener
//var helloButton_onClick = function(event) {
//    alert('Hello world and hello ' +
//        dojo.byId("txtName").value + '!');
//}
//
////start initializing
////dojo.addOnLoad(init);  //<1.7
//require(["dojo/ready"], function(ready){
//  ready(init);
//});